﻿
using IdentityModel;
using IdentityModel.Client;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.Identity;
using Models.JsonResultModel;
using Models.WebClient;
using SqlSugar;
using System;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Net.Http.Headers;
using System.Security.Principal;
using UtilsORM;
using UtilsORM.MySqlData;
using UtilsORM.MySqlData;
using ServiceWeb.APIRequest;
using ServiceWeb.DBGateway;
using UtilsBase;
using UtilsORM.MySqlData;
using GatewayServer.Controllers.API.Common;
namespace GatewayServer.Controllers.API.Mobile
{
    [Route("api/mobile/[controller]/[action]")]
    [ApiController]
    //[BasicAuthentication]
    public class TaskTimeFixedController : ApiCommonControllerBase
    {


        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="dguid"></param>
        /// <returns></returns>
        /// 
        
        public RespResultData getListByClient()
        {
            string cguid = baseClientLoginUserInfo.cGuid;
            return RespResult.Sucess(new DBIotDeviceTaskTimeFixed().getListByClient(cguid));
        }
        
        public RespResultData getListByProduct(string pguid)
        {

            return RespResult.Sucess(new DBIotDeviceTaskTimeFixed().getListByProduct(pguid));
        }
        
        public RespResultData getModel(string tfguid)
        {

            return RespResult.Sucess(new DBIotDeviceTaskTimeFixed().getModel(tfguid));
        }
        
        public RespResultData delFixed(string tfguid)
        {
            iot_device_task_time_fixed time_Fixed = new DBIotDeviceTaskTimeFixed().getModel(tfguid);

            new DBIotDeviceTaskPlain().delPlain(time_Fixed.tf_task_guid);
            new DBIotDeviceTaskPlainItem().delItemAll(time_Fixed.tf_task_guid);
            new DBIotDeviceTaskTimeFixed().delModel(tfguid);
            return RespResult.Sucess();
        }
        
        public RespResultData saveFixed(string order, string pguid, string timevalue, int status, string tfguid, [FromForm] string title, [FromForm] string tf_interval_day = "0")
        {
            string cguid = baseClientLoginUserInfo.cGuid;


            iot_device_task_plain plain = new iot_device_task_plain();
            if (order == "add")
            {
                plain.tp_guid = Guid.NewGuid().ToString();
                plain.tp_p_guid = pguid;
                plain.tp_c_guid = cguid;
                plain.tp_title = title;
                plain.tp_status = 0;
            }
            else
            {
                iot_device_task_time_fixed time_Fixed = new DBIotDeviceTaskTimeFixed().getModel(tfguid);
                plain.tp_guid = time_Fixed.tf_task_guid;
                plain.tp_title = title;
            }

            new DBIotDeviceTaskPlain().saveAll(plain);



            iot_device_task_time_fixed req = new iot_device_task_time_fixed();
            if (order == "add")
            {
                req.tf_guid = Guid.NewGuid().ToString();
                req.tf_p_guid = pguid;
                req.tf_c_guid = baseClientLoginUserInfo.cGuid.ToString();
                req.tf_task_guid = plain.tp_guid;
                req.tf_task_type = "taskplain";
            }
            else
            {
                req.tf_guid = tfguid;
                req.tf_task_guid = plain.tp_guid;
            }
            //req.tf_p_guid

            req.tf_c_guid = cguid;
            req.tf_time_value = timevalue;
            req.tf_status = status;
            req.tf_title = title;
            req.tf_interval_day = int.Parse(tf_interval_day);
            new DBIotDeviceTaskTimeFixed().saveAll(req);
            return RespResult.Sucess(plain.tp_guid);
        }

    }
}
